package com.dolphin.common.component.var.model;

import com.dolphin.common.component.var.enums.*;
import com.dolphin.common.expr.Condition;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Getter
@Setter
@ToString
public class TimeWindowVarModel extends ExtVarModel implements VarWithDimensions{
    private static final String TIME_WINDOW_CATEGORY = "WINDOW";

    private CalculationEnum calculationType;
    private String conditionsJson;
    private Boolean includeCurrentPeriod;
    private String startTimeTypeBid;
    private String timeVarCode;
    private BaseVarModel timeVarModel;
    private String endTimeTypeBid;
    private String statisticDimensionBids;
    private String dimensionVarCode;
    private BaseVarModel dimensionVarModel;
    private String statisticVarBid;
    private String statisticVarCode;
    private BaseVarModel statisticVarModel;
    private ValueTypeEnum statisticVarValueType;
    private Integer timeSliceNum;
    private TimeSliceUnitEnum timeSliceUnit;
    private int slotCount;
    private long slotMillis;
    private PeriodTypeEnum periodType;
    private NaturePeriodTypeEnum naturePeriodType;
    private Condition condition;
    private RangePeriodTypeEnum rangePeriodType;
    private int rangeStart;
    private int rangeEnd;
    private Integer indexNum;
    private String rankVarBid;
    private BaseVarModel rankVarModel;

    private boolean write;
    private boolean read;

    public TimeWindowVarModel() {
    }

    @Override
    public String getBid() {
        return VarTypeEnum.TIME_WINDOW_VAR.getIndex() + getId();
    }

    @Override
    public List<String> fetchDimensionVarBids() {
        String bids = getStatisticDimensionBids();
        return StringUtils.isNotEmpty(bids) ? Arrays.asList(bids.split(","))
                : Collections.emptyList();
    }
    @Override
    public VarTypeEnum getVarType() {
        return VarTypeEnum.TIME_WINDOW_VAR;
    }

    public boolean isLastIndex(){
        return "lastIndex".equalsIgnoreCase(this.getCalculationType().getCode());
    }

    public boolean isAggregation(){
        return calculationType == CalculationEnum.SUM
                || calculationType == CalculationEnum.COUNT
                || calculationType == CalculationEnum.AVG
                || calculationType == CalculationEnum.MAX
                || calculationType == CalculationEnum.MIN;
    }

    public boolean isTop(){
        return calculationType == CalculationEnum.TOP
                || calculationType == CalculationEnum.TOP_N_MAX
                || calculationType == CalculationEnum.TOP_N_MIN;
    }

    public Integer getIndexNum() {
        return indexNum != null && indexNum > 0 ? indexNum
                : calculationType.getValue();
    }

    public long getSlotMillis() {
        return slotMillis != 0 ? slotMillis
                : (timeSliceUnit != null ? timeSliceUnit.getTime() : 0);
    }

    public int getSlotCount(){
        return slotCount != 0 ? slotCount
                : (timeSliceNum != null ? timeSliceNum : 0);
    }


}
